library(tidyverse)
# Load the necessary libraries
library(rnaturalearth)
library(rnaturalearthdata)
library(ggplot2)
library(dplyr)
library(ggthemes)## Warning: package 'ggthemes' was built under R version 4.3.3
## Warning: package 'plotly' was built under R version 4.3.3
Insert any text here.
# Insert code for Exercise 1 here
#| crop = true
# Get the world map data
world_map <- ne_countries(scale = "medium", returnclass = "sf") |>
filter(name != "Antarctica")
renewable_grow <- read.csv("annual-percentage-change-renewables.csv") |>
filter(Code != "NA")
renewable_grow$Renewables....growth. <- round(renewable_grow$Renewables....growth.,2)
renewable_plot <- function(region, year) {
renewable_grow22 <- renewable_grow |>
filter(Year == year)
renewable_grow_map22 <- left_join(world_map,renewable_grow22,
c( "iso_a3_eh"= "Code")) |>
rename( "growth" = "Renewables....growth.")
if (region == "World") {
} else if (region == "North America" | region == "Latin America & Caribbean") {
renewable_grow_map22 <- filter(renewable_grow_map22, region_wb == region)
}
else {renewable_grow_map22 <- filter(renewable_grow_map22, region_un == region )}
renewable_grow_map22 |>
mutate(text = paste("<b>",admin,"<b>\n growth rate:",signif(growth,2)))
p <- ggplot(data = renewable_grow_map22) +
geom_sf( aes( fill = growth + runif(nrow(renewable_grow_map22),min = 0, max = 0.001), text = paste("<b>",admin,"<b>\n growth rate:",signif(growth,2))), color = "black")+
#scale_fill_manual("", values = my_colors)+
labs(
title = paste("Annual percentage change in renewable energy generation in", year),
fill = "Growth %") +
scale_fill_continuous(low = "red", high = "blue", limits = c(-20, 20), guide = guide_colorbar(direction = "horizontal", ticks = TRUE, nbin = 50, barwidth = 10, barheight = 1, label.position = "bottom", title.position = "top", title.hjust = 0.5)) +
theme_map()
#scale_fill_continuous(low = "red", mid = "white", high = "blue", labels = scales::percent)
ggplotly(p, tooltip = "text") |>
style(hoveron = "fills")
}
renewable_plot("World", 2022)## Warning in layer_sf(geom = GeomSf, data = data, mapping = mapping, stat = stat,
## : Ignoring unknown aesthetics: text
energy_gen <- read.csv("modern-renewable-prod.csv")
egen_plot <- function(region) {
energy_gen_spec_line <- energy_gen |>
filter(Entity == region) |>
rename( "Wind"="Electricity.from.wind...TWh" ,"Hydro" = "Electricity.from.hydro...TWh", "Solar" = "Electricity.from.solar...TWh","Other" = "Other.renewables.including.bioenergy...TWh")
energy_gen_spec_point <- energy_gen_spec_line |>
pivot_longer(cols = -c(Entity, Code, Year), names_to = "sources",
values_to = "Energy" )
p <- ggplot() +
geom_line(data = energy_gen_spec_line,aes(x = Year, y = Wind), color = "purple") +
geom_line(data = energy_gen_spec_line,aes(x = Year, y = Hydro), color = "pink") +
geom_line(data = energy_gen_spec_line,aes(x = Year, y = Solar), color = "green") +
geom_line(data = energy_gen_spec_line,aes(x = Year, y = Other), color = "orange") +
geom_point(data = energy_gen_spec_point, aes(x = Year, y = Energy, color = sources), size = 2)
ggplotly(p, tooltip = c("Energy","Year"))
}
egen_plot("North America")…